package net.wanho.personalOffice.controller;

import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.personalOffice.po.Comment;
import net.wanho.personalOffice.po.DailyRecord;
import net.wanho.personalOffice.po.IndividualTask;
import net.wanho.personalOffice.service.DailyRecordService;
import net.wanho.personalOffice.service.IndividualTaskService;
import net.wanho.systemSettings.po.Employee;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/DailyRecordServlet")
public class DailyRecordServlet extends BaseServlet<DailyRecordService> {
    /*
    查找
     */
    protected String list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            List<DailyRecord> dailyRecordList = super.baseService.findAll();
            request.setAttribute("dailyRecordList",dailyRecordList);
            return "/view/personalOffice/dailylog/index.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    protected String view(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String recordId = request.getParameter("recordId");
            DailyRecord dailyRecord = super.baseService.findById(recordId);
            request.setAttribute("dailyRecord",dailyRecord);
            return "/view/personalOffice/dailylog/view.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
    protected String del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return null;
    }
    protected String upt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return null;
    }
    protected String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            DailyRecord dailyRecord = super.parseRequest(request, DailyRecord.class);
            super.baseService.save(dailyRecord);
            return "redirect:/DailyRecordServlet?method=list";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
    protected String addComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Comment comment = super.parseRequest(request, Comment.class);
            String ownerId = request.getParameter("ownerId");
            comment.getCommentEmployee().setEmployeeId(Integer.parseInt(ownerId));
            super.baseService.saveComment(comment);
            DailyRecord dailyRecord = super.baseService.findById(comment.getRecordId()+"");
            request.setAttribute("dailyRecord",dailyRecord);
            return "/view/personalOffice/dailylog/view.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
}
